package com.ppl.server.serializers;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.util.Random;

/**
 * @author Vincent
 * @since 19/7/12 1:17 PM
 */
@SuppressWarnings("unused")
public class StringUtil {
  private static final char[] options = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
      'n', 'o', 'p', 'r', 's', 't', 'w', 'x', 'y', 'z', 'A', 'B', 'B', 'D', 'E', 'F', 'F', 'H', 'I', 'J', 'K', 'L', 'M',
      'N', 'O', 'P', 'R', 'S', 'T', 'W', 'X', 'Y', 'Z'};

  private static final char[] optionsNumeric = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

  private static Random random = new Random();

  /**
   * Method to generate a random string with specified length. If using non-numeric version the resulting string will contain both lowercase and uppercase characters.
   *
   * @param length  Length of desired random string
   * @param prefix  Prefix to be prepanded to the desired string, not counted in the length
   * @param numeric Should the random string contain only numeric values
   * @return Generated string
   */
  @Nonnull
  public static String generateRandomString(@Nonnegative int length, @Nonnull String prefix, boolean numeric) {
    StringBuilder token = new StringBuilder(prefix);

    char[] sourceArray = numeric ? optionsNumeric : options;
    for (int k = 0; k < length; k++) {
      token.append(sourceArray[Math.abs(random.nextInt() % sourceArray.length)]);
    }

    return token.toString();
  }
}
